1537C - Challenging Cliffs - CodeForces Solution


constructive algorithms greedy implementation math *1200

Please click on ads to support us..

Python Code:

import sys
inp = sys.stdin.readline
input = lambda: inp().strip()
ii = lambda: int(input())
mi = lambda: map(int,input().split())
li = lambda: list(map(int,input().split()))
ms = lambda: map(input().split())
ls = lambda: list(input().split())
from collections import defaultdict,Counter,deque
from bisect import bisect_left,bisect_right
from heapq import heappush,heappop,heapify

for _ in range(ii()):
    n = ii()
    nums = li()
    nums.sort()
    mins = (-1,-1,float('inf'))
    for i in range(1,len(nums)):
        if nums[i] - nums[i-1] < mins[2]:
            mins = (i-1,i,nums[i]-nums[i-1])
    print(nums[mins[0]],end=' ')
    for i in range(mins[1]+1,len(nums)):
        print(nums[i],end=' ')
    for j in range(mins[0]):
        print(nums[j], end=' ')
    print(nums[mins[1]])

C++ Code:

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<int>> vvi;
typedef map<int, int> mi;
typedef map<ll, ll> ml;
typedef set<int> si;
typedef set<ll> sl;
typedef pair<int, int> pi;
typedef unordered_set<int> uis;
typedef unordered_set<ll> uls;
typedef pair<ll, ll> pl;


#define PA(a, n) for(int i = 0; i < n; i++) cout << a[i] << ' '
#define PV(v) for(auto i : v) cout << i << ' '
#define YES cout << "YES\n"
#define NO cout << "NO\n"


int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t;
  cin >> t;
  while(t--){
    ll n;
    cin >> n;

    ll h[n];
    for(ll i = 0; i <n; i++) cin >> h[i];

    sort(h, h + n);

    ll ans[n];

    ll md = LLONG_MAX;
    ll ind = 0;

    for(ll i = 0; i <= n-2; i++){
      if(abs(h[i] - h[i+1]) < md){
        md = abs(h[i]-h[i+1]);
        ind = i;
      }
    }

    ans[0] = h[ind];
    ans[n-1] = h[ind+1];

    ll ci = (ind + 2) % n;
    for(ll i = 1; i <= n-2; i++){
      ans[i] = h[ci];
      ci = (ci+1) % n;
    }

    PA(ans, n);
    cout << '\n';

  }

  return 0;
}


Comments

Submit
0 Comments
More Questions

1717B - Madoka and Underground Competitions
61B - Hard Work
959B - Mahmoud and Ehab and the message
802G - Fake News (easy)
1717C - Madoka and Formal Statement
420A - Start Up
1031A - Golden Plate
1559C - Mocha and Hiking
427B - Prison Transfer
330A - Cakeminator
426A - Sereja and Mugs
363A - Soroban
1585C - Minimize Distance
1506E - Restoring the Permutation
1539A - Contest Start
363D - Renting Bikes
1198D - Rectangle Painting 1
1023B - Pair of Toys
1725A - Accumulation of Dominoes
1675E - Replace With the Previous Minimize
839A - Arya and Bran
16B - Burglar and Matches
1625B - Elementary Particles
1725G - Garage
1725B - Basketball Together
735A - Ostap and Grasshopper
1183B - Equalize Prices
1481A - Space Navigation
1437B - Reverse Binary Strings
1362B - Johnny and His Hobbies